package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.dao.AlipayMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.AlipayMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.AlipayMchntInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.AlipayActivityApplyConfirmRequest;
import cc.rengu.igas.bomp.facade.response.AlipayActivityApplyConfirmResponse;
import cc.rengu.igas.channel.alipay.facade.request.ActivityConfirmRequest;
import cc.rengu.igas.channel.alipay.facade.response.ActivityConfirmResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付宝特殊费率申请确认
 * Created by moxm
 */
public class AlipayActivityApplyConfirmService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        AlipayActivityApplyConfirmRequest alipayActivityApplyConfirmRequest = new AlipayActivityApplyConfirmRequest();
        ConvertUtil.convertOutput(alipayActivityApplyConfirmRequest);
        return alipayActivityApplyConfirmRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        AlipayActivityApplyConfirmRequest alipayActivityApplyConfirmRequest = (AlipayActivityApplyConfirmRequest) request;
        AlipayActivityApplyConfirmResponse alipayActivityApplyConfirmResponse = new AlipayActivityApplyConfirmResponse();
        BizResponse<AlipayActivityApplyConfirmResponse> bizResponse = new BizResponse<>();
        String instId = alipayActivityApplyConfirmRequest.getHeader().getInstId();
        /*用户标识*/
        String userId = alipayActivityApplyConfirmRequest.getUserId();
        /*商户号*/
        String mchntNo = alipayActivityApplyConfirmRequest.getMchntNo();
        /*特殊费率申请ID*/
        String activityApplyId = alipayActivityApplyConfirmRequest.getActivityApplyId();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo)) {
            rglog.error("支付宝特殊费率申请确认交易必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        if (CollectionUtils.isEmpty(txnInfo.getChannelCallCfgList()) || txnInfo.getChannelCallCfgList().size() < 1) {
            rglog.error("第三方机构商户进件处理失败，txnInfo.getchannelCallCfgList 通道服务调用配置为空!");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }

        /* 获取通道标识 */
        List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList();
        List<ChannelCallCfg> msgDstchannelCallCfgList = channelCallCfgList.stream().filter(item -> item.getCallChannelId().equals("ALIP")).collect(Collectors.toList());
        ChannelCallCfg channelCallCfg = msgDstchannelCallCfgList.get(0);
        String channelId = channelCallCfg.getCallChannelId();

        /*获取通道类型*/
        AlipayMchntInfoMapper alipayMchntInfoMapper = new AlipayMchntInfoMapperImpl();
        AlipayMchntInfo alipayMchntInfo = alipayMchntInfoMapper.selectAlipayMchntInfoByPrimaryKey(instId, mchntNo);
        if (null == alipayMchntInfo) {
            rglog.error("支付宝商户信息为空!");
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        String channelType = alipayMchntInfo.getDstChannelType();

        /*设置报文头*/
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        cc.rengu.igas.channel.alipay.facade.base.Header header = new cc.rengu.igas.channel.alipay.facade.base.Header();
        header.setInstId(instId);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setTraceNo(sysSeqNum + channelCallCfg.getCallIndex());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setChanlId(BompAppParamConstant.BOMP_SYS_ID);
        header.setBizType(channelCallCfg.getBizType());
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());

        ActivityConfirmRequest activityConfirmRequest = new ActivityConfirmRequest();
        activityConfirmRequest.setChannelId(channelId);
        activityConfirmRequest.setChannelType(channelType);
        activityConfirmRequest.setActivityId(activityApplyId);

        /* 调用dobbu服务并接收响应信息 */
        DubboService dubboService = new DubboServiceImpl();

        Result<ActivityConfirmResponse> result = (Result<ActivityConfirmResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_ALIPAY_MCHNT_SERVICE, "activityConfirm", activityConfirmRequest);
        if (null == result || !result.isSuccess()) {
            rglog.error("调dobbu失败，respCode={},respDesc={}", OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            bizResponse.setResult(alipayActivityApplyConfirmResponse);
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            bizResponse.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            return bizResponse;
        } else if (!OltpRpcdEnum.TRANS_SUCCESS.getRespCode().equals(result.getResult().getRespCode())) {
            alipayActivityApplyConfirmResponse.setRespCode(result.getResult().getRespCode());
            alipayActivityApplyConfirmResponse.setRespDesc(result.getResult().getRespDesc());
            bizResponse.setResult(alipayActivityApplyConfirmResponse);
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            bizResponse.setRespCode(result.getResult().getRespCode());
            bizResponse.setRespDesc(result.getResult().getRespDesc());
            return bizResponse;
        }

        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(alipayActivityApplyConfirmResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        AlipayActivityApplyConfirmResponse alipayActivityApplyConfirmResponse = (AlipayActivityApplyConfirmResponse) bizResponse.getResult();
        ConvertUtil.convertInput(alipayActivityApplyConfirmResponse);
    }
}
